var router = require('express').Router();
var fs = require('fs');
var path = require('path');

router.get('/:page', function(req, res, next){
    var page = req.params.page;
    var page_path = path.join(req.app.get('views'), 'pages', page + '.jade');
    if(typeof page === 'undefined')
	notFound(next);
    else{
	fs.exists(page_path, function(exists){
	    if(exists)
		res.render('pages/' + page, {});
	    else
		notFound(next);
	});
    }
});

function notFound(next){
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
}

module.exports = router;
